package dhz.storage.controllers;

import dhz.storage.dao.ShareLinkDao;
import dhz.storage.models.ShareLink;
import dhz.storage.util.JwtUtil;
import dhz.storage.util.JwtRedisUtil;
import dhz.storage.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
@Api(tags = "测试接口")
@RequestMapping("/test")
public class TestControllers {
    @Resource
    JwtRedisUtil util;

    @Resource
    ShareLinkDao dao;

    /**
     * 用来测试Swagger-UI有没有调试好
     * */
    @PostMapping("/swagger-test")
    public String swaggerTest(@RequestParam("string")String s){
        return s;
    }

    /**
     * 用来测试spring与redis的交互有没有调整好
     * */
    @PostMapping("/redis-test")
    public boolean redisTest(@RequestParam("number")String number){
        return util.hasID(number);
    }

    /**
     * 用来测试jwt的编码和解码
     * */
    @PostMapping("/jwt-test")
    public Response jwtTest(HttpServletResponse response, @RequestParam("identification")String s){
        String ident = JwtUtil.getToken(s);
        HashMap<String, String> data = new HashMap<>();
        data.put("jwt", ident);
        response.setHeader("Authorization", ident);
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        System.out.println(JwtUtil.getIdentification(ident));
        Response res = new Response(data);
        return res;
    }

    /**
     * 网盘部分并不需要单独的用户登录，这个接口只是用来模拟用户登录
     * */
    @PostMapping("/login")
    @ApiOperation(value = "用来模拟用户登录的一个接口，生成相关jwt在redis中")
    public Response simLogin(@RequestParam("identification") String identification, HttpServletResponse response){
        // 获得用户的jwt
        String jwt = JwtUtil.getToken(identification);
        util.addToken(identification);
        // 设置相关的响应头
        response.setHeader("Authorization", jwt);
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        HashMap<String,String> data = new HashMap<>();
        data.put("jwt", jwt);
        Response res = new Response(1001, data);
        return res;
    }

    /**
     * 这是一个必须登录才能访问的接口
     * */
    @PostMapping("/logged")
    public Response logged(){
        String s = SecurityUtils.getSubject().getPrincipal().toString();
        Response res = new Response(s);
        return res;
    }

    /**
     * 用来测试MongoDB能不能往里面添加链接
     * @param identification 学号
     * @param path 被分享的位置
     * @return 一个response对象
     * */
    @PostMapping("/addLink")
    public Response addDoc(@RequestParam("identification") String identification, @RequestParam("path") String path){
        ShareLink link = new ShareLink(identification, path);
        long id = dao.save(link);
        Response result = new Response(null);
        result.setCode(1001);
        result.setData(id);
        return result;
    }

    /**
     * 返回一个用户全部分享链接，用来展示刚刚添加的能不能被发现
     * */
    @PostMapping("/findLink")
    public Response findLinks(@RequestParam("identification")String identification){
        Response result = new Response(dao.findShareLinks(identification, 0));
        result.setCode(1001);
        return result;
    }
}
